//
//  setPhoneViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/7/28.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "setPhoneViewController.h"
#import "phoneCodeViewController.h"
#import "AFNetworking.h"
#import "AFHTTPRequestOperationManager.h"

@interface setPhoneViewController ()

@property (nonatomic,strong) UITextField *phoneTextField;

@end

@implementation setPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

#pragma mark ----------------------- init ---------------------

- (void)initView{
    
    self.title = @"输入手机号";
    
    UILabel *leftLabel = [ConfigXp labelWithFrame:CGRectZero
                                    text:@"中国（+86）"
                               textColor:getColor(@"4a4a4a")
                                    font:get4sFont(12)
                           textAlignment:NSTextAlignmentLeft
                         backgroundColor:[UIColor clearColor]];
    [self.view addSubview:leftLabel];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(31));
        make.left.mas_equalTo(self.view).with.offset(screenScale(25));
        
        
    }];
    
    UIImageView *sexImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow_icon"]];
    [self.view addSubview:sexImg];
    [sexImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(32));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-30));
        make.width.mas_equalTo(screenScale(12));
        make.height.mas_equalTo(screenScale(12));
    }];
    
    UIView *sexThread = [[UIView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(62), scrWidth() - screenScale(40), screenScale(0.5))];
    sexThread.backgroundColor = getThreadColor();
    [self.view addSubview:sexThread];
    
    _phoneTextField = [[UITextField alloc]init];
    _phoneTextField.placeholder = @"请输入手机号码";
    _phoneTextField.textColor = getColor(@"4a4a4a");
    _phoneTextField.font = get4sFont(12);
    _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    _phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_phoneTextField setValue:getColor(@"4a4a4a") forKeyPath:@"_placeholderLabel.textColor"];
    [self.view addSubview:_phoneTextField];
    [_phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(80));
        make.left.mas_equalTo(self.view).with.offset(screenScale(24));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-30));
        
    }];
    
    UIView *phoneThread = [[UIView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(112), scrWidth() - screenScale(40), screenScale(0.5))];
    phoneThread.backgroundColor = getThreadColor();
    [self.view addSubview:phoneThread];
    
    UIButton *nextBtn = [ConfigXp buttonWithFrame:CGRectMake(screenScale(20), screenScale(138), scrWidth() - screenScale(40), screenScale(44))
                                            title:@"继续"
                                       titleColor:getColor(@"ffffff")
                                             font:get4sFont(14)
                                  backgroundColor:getColor(@"3a95ff")
                                            image:nil];
    nextBtn.layer.cornerRadius = screenScale(3);
    [nextBtn addTarget:self action:@selector(nextBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextBtn];
}

- (void)nextBtnClick{
    
    if (![TSRegularExpressionUtil validateMobile:_phoneTextField.text]) {
        
        [PublicFunction showAlert:nil message:@"请输入正确格式的手机号码" duration:1.0];
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: _phoneTextField.text forKey:@"phone"];
    [productParams setValue: _pwd forKey:@"pass"];
    [productParams setValue: _user_name forKey:@"user_name"];
    [productParams setValue: _birthday forKey:@"birthday"];
    [productParams setValue: _sex forKey:@"sex"];
    
    //两种方式上传头像
    /*方式一：使用NSData数据流传图片*/
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/plain",
                                                         @"text/javascript",
                                                         @"text/json",
                                                         @"text/html",
                                                         @"image/jpeg", nil];
    [manager POST:login_register parameters:productParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        //压缩图片
        
        //拼接图片名称+时间确保不重复命名
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *imageFileName = [NSString stringWithFormat:@"%@.jpg", [formatter stringFromDate:[NSDate date]]];
        // 测试输出文件名
        
        //上传图片，以文件流的格式sdf
        
        [formData appendPartWithFileData:UIImageJPEGRepresentation(_header_img, 5.0) name:@"header_img" fileName:imageFileName mimeType:@"image/jpeg"];
        
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSString *strtmp = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSString *requestTmp = [NSString stringWithString: strtmp];
        NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];  //解析
        
//        [self.navigationController popToRootViewControllerAnimated:YES];
        
        
        
        NSLog(@"+++>%@", resultDic);
        
        NSLog(@"--->%@", resultDic[@"msg"]);
        
        [hud removeFromSuperview];
        
        phoneCodeViewController *vc = [[phoneCodeViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"--->%@", error);
        
        [hud removeFromSuperview];
        
    }];
    
}

@end
